<?php 

namespace fast;

use think\queue\Job;
use think\Db;

class Reward{
    
    public function fire(Job $job, $data){
    
         
          $isJobDone = $this->doJob($data);
          
          if ($isJobDone) {
              $job->delete();
          }else{
              if ($job->attempts() > 3) {
                  file_put_contents('attempts.txt', time(), FILE_APPEND);
                  $job->delete();
                  // 也可以重新发布这个任务
                  //print("<info>Hello Job will be availabe again after 2s."."</info>\n");
                  //$job->release(2); //$delay为延迟时间，表示该任务延迟2秒后再执行
              }
          }
          
    }
    
    public function doJob($data){
      if($data){
        // 启动事务
        Db::startTrans();
        $now =time();
        $createtime = strtotime(date("Y-m-d H",$now).":00:00");//
        try{
            $user_miner = Db::name('user_miner')
            ->where(['id'=>['in',$data]])
            ->where('miner_cat','not in','2,4,5')
            ->select();

            foreach ($user_miner as $key => $value) 
            {
              $invite = Db::name('invite')->where('user_id',$value['user_id'])->find();
              $status = Db::name("user")->where('id', $value['user_id'])->value('status');
              if($status!='normal'){
                  continue;
              }

              $all_parentid = explode(',', $invite['path']);
              $all_invite_user = Db::name('user')
              ->where('id','in',$all_parentid)
              ->where('status','normal')
              ->field('id,total_zt_person,movable_wallet,ipfs_wallet')
              ->order('id desc')
              ->select(); 

              $generation = 0;
              foreach ($all_invite_user as $k => $v)
              {
                if($v['id'] == $value['user_id']){
                    continue;
                }else{
                  $generation++;
                }

                switch ($generation) {
//                  case 1:
//                        if($v['total_zt_person']>=1){
//                            $is_buy = Db::name('user_miner')
//                            ->where(['user_id'=>$v['id'],'miner_id'=>$value['miner_id']])
//                            ->find();
//                            if($is_buy){
//                              //加速奖励
//                              switch ($is_buy['miner_type']){
//                                  case 1:
//                                      $number = 0.004;
//                                      break;
//                                  case 2:
//                                      $number = 0.003;
//                                      break;
//                                  case 3:
//                                      $number = 0.002;
//                                      break;
//                                  case 4:
//                                      $number = 0.005;
//                                      break;
//                                  default:
//                                      $number = 0.004;
//                              }
//                              $user_miner = [];
//                              $user_miner['virtual']      = 1;
//                              $user_miner['from_id']      = $value['id'];
//                              $user_miner['user_id']      = $v['id'];
//                              $user_miner['miner_id']     = 0;
//                              $user_miner['miner_number'] = 1;
//                              $user_miner['output_value'] = $value['output_value']*$number;
//                              $user_miner['arithmetic_value'] = $value['arithmetic_value']*$number;
//                              $user_miner['sum_earnings'] = 0;
//                              $user_miner['price']        = 0;
//                              $user_miner['buy_type']     = 0;
//                              $user_miner['miner_cat']    = 0;
//                              $user_miner['miner_type']   = 0;
//                              $user_miner['run_cycle']    = 240;
//                              $user_miner['createtime']   = $createtime;
//                              $user_miner['endtime']      = $createtime+10*3600*24;
//                              Db::name('user_miner')->insert($user_miner);
//                            }
//
//                        }
//                        break;
//                  case 2:
//                        if($v['total_zt_person']>=2){
//                            $is_buy = Db::name('user_miner')
//                            ->where(['user_id'=>$v['id'],'miner_id'=>$value['miner_id']])
//                            ->find();
//                            if($is_buy){
//                              //加速奖励
//                                switch ($is_buy['miner_type']){
//                                    case 1:
//                                        $number = 0.003;
//                                        break;
//                                    case 2:
//                                        $number = 0.002;
//                                        break;
//                                    case 3:
//                                        $number = 0.001;
//                                        break;
//                                    case 4:
//                                        $number = 0.004;
//                                        break;
//                                    default:
//                                        $number = 0.004;
//                                }
//                              $user_miner = [];
//                              $user_miner['virtual']      = 1;
//                              $user_miner['from_id']      = $value['id'];
//                              $user_miner['user_id']      = $v['id'];
//                              $user_miner['miner_id']     = 0;
//                              $user_miner['miner_number'] = 1;
//                              $user_miner['output_value'] = $value['output_value']*$number;
//                              $user_miner['arithmetic_value'] = $value['arithmetic_value']*$number;
//                              $user_miner['sum_earnings'] = 0;
//                              $user_miner['price']        = 0;
//                              $user_miner['buy_type']     = 0;
//                              $user_miner['miner_cat']    = 0;
//                              $user_miner['miner_type']   = 0;
//                              $user_miner['run_cycle']    = 240;
//                              $user_miner['createtime']   = $createtime;
//                              $user_miner['endtime']      = $createtime+10*3600*24;
//                              Db::name('user_miner')->insert($user_miner);
//                            }
//                        }
//                        break;
                    case 4:
                        if($v['total_zt_person']>=4){
                            //见点奖励
                            $invite_reward = $value['price']*2/100;
                            $invite_reward = round($invite_reward,5);
                            if($invite_reward>0){
                                $last_invite_reward = $invite_reward*0.8;
                                $user_balance_log[0]['user_id']    = $v['id'];
                                $user_balance_log[0]['amount']     = $last_invite_reward;
                                $user_balance_log[0]['before']     = $v['movable_wallet'];
                                $user_balance_log[0]['after']      = $v['movable_wallet']+$last_invite_reward;
                                $user_balance_log[0]['remark']     = '见点奖励';
                                $admin_remark = "代数[{$generation}],直推人数[{$v['total_zt_person']}],来源用户ID[{$value['user_id']}],来源矿机ID[{$value['id']}]";
                                $user_balance_log[0]['admin_remark']   = $admin_remark;
                                $user_balance_log[0]['type']       = 1;
                                $user_balance_log[0]['log_type']   = 3;
                                $user_balance_log[0]['source']     = 4;
                                $user_balance_log[0]['createtime'] = time();

                                $ipfs_reward = $invite_reward*0.2;
                                $user_balance_log[1]['user_id']    = $v['id'];
                                $user_balance_log[1]['amount']     = $ipfs_reward;
                                $user_balance_log[1]['before']     = $v['ipfs_wallet'];
                                $user_balance_log[1]['after']      = $v['ipfs_wallet']+$ipfs_reward;
                                $user_balance_log[1]['remark']     = '见点奖励';
                                $admin_remark = "代数[{$generation}],直推人数[{$v['total_zt_person']}],来源用户ID[{$value['user_id']}],来源矿机ID[{$value['id']}]";
                                $user_balance_log[1]['admin_remark']   = $admin_remark;
                                $user_balance_log[1]['type']       = 1;
                                $user_balance_log[1]['log_type']   = 7;
                                $user_balance_log[1]['source']     = 4;
                                $user_balance_log[1]['createtime'] = time();

                                Db::name('user')->where('id',$v['id'])
                                    ->update([
                                        'movable_wallet' => Db::raw('movable_wallet+'.$last_invite_reward),
                                        'ipfs_wallet' => Db::raw('ipfs_wallet+'.$ipfs_reward),
                                        'total_jian_reward' => Db::raw('total_jian_reward+'.$invite_reward),
                                    ]);
                                Db::name('user_balance_log')->insertAll($user_balance_log);
                            }
                        }
                        break;
                    case 5:
                    case 6:
                    case 7:
                    if($v['total_zt_person']>=5){
                        //见点奖励
                        $invite_reward = $value['price']*1/100;
                        $invite_reward = round($invite_reward,5);
                        if($invite_reward>0){
                            $last_invite_reward = $invite_reward*0.8;
                            $user_balance_log[0]['user_id']    = $v['id'];
                            $user_balance_log[0]['amount']     = $last_invite_reward;
                            $user_balance_log[0]['before']     = $v['movable_wallet'];
                            $user_balance_log[0]['after']      = $v['movable_wallet']+$last_invite_reward;
                            $user_balance_log[0]['remark']     = '见点奖励';
                            $admin_remark = "代数[{$generation}],直推人数[{$v['total_zt_person']}],来源用户ID[{$value['user_id']}],来源矿机ID[{$value['id']}]";
                            $user_balance_log[0]['admin_remark']   = $admin_remark;
                            $user_balance_log[0]['type']       = 1;
                            $user_balance_log[0]['log_type']   = 3;
                            $user_balance_log[0]['source']     = 4;
                            $user_balance_log[0]['createtime'] = time();

                            $ipfs_reward = $invite_reward*0.2;
                            $user_balance_log[1]['user_id']    = $v['id'];
                            $user_balance_log[1]['amount']     = $ipfs_reward;
                            $user_balance_log[1]['before']     = $v['ipfs_wallet'];
                            $user_balance_log[1]['after']      = $v['ipfs_wallet']+$ipfs_reward;
                            $user_balance_log[1]['remark']     = '见点奖励';
                            $admin_remark = "代数[{$generation}],直推人数[{$v['total_zt_person']}],来源用户ID[{$value['user_id']}],来源矿机ID[{$value['id']}]";
                            $user_balance_log[1]['admin_remark']   = $admin_remark;
                            $user_balance_log[1]['type']       = 1;
                            $user_balance_log[1]['log_type']   = 7;
                            $user_balance_log[1]['source']     = 4;
                            $user_balance_log[1]['createtime'] = time();

                            Db::name('user')->where('id',$v['id'])
                                ->update([
                                    'movable_wallet' => Db::raw('movable_wallet+'.$last_invite_reward),
                                    'ipfs_wallet' => Db::raw('ipfs_wallet+'.$ipfs_reward),
                                    'total_jian_reward' => Db::raw('total_jian_reward+'.$invite_reward),
                                ]);
                            Db::name('user_balance_log')->insertAll($user_balance_log);
                        }
                    }
                    break;
                    case 8:
                    case 9:
                        if($v['total_zt_person']>=5){
                                //见点奖励
                                $invite_reward = $value['price']*0.5/100;
                                $invite_reward = round($invite_reward,5);
                                if($invite_reward>0){
                                    $last_invite_reward = $invite_reward*0.8;
                                    $user_balance_log[0]['user_id']    = $v['id'];
                                    $user_balance_log[0]['amount']     = $last_invite_reward;
                                    $user_balance_log[0]['before']     = $v['movable_wallet'];
                                    $user_balance_log[0]['after']      = $v['movable_wallet']+$last_invite_reward;
                                    $user_balance_log[0]['remark']     = '见点奖励';
                                    $admin_remark = "代数[{$generation}],直推人数[{$v['total_zt_person']}],来源用户ID[{$value['user_id']}],来源矿机ID[{$value['id']}]";
                                    $user_balance_log[0]['admin_remark']   = $admin_remark;
                                    $user_balance_log[0]['type']       = 1;
                                    $user_balance_log[0]['log_type']   = 3;
                                    $user_balance_log[0]['source']     = 4;
                                    $user_balance_log[0]['createtime'] = time();

                                    $ipfs_reward = $invite_reward*0.2;
                                    $user_balance_log[1]['user_id']    = $v['id'];
                                    $user_balance_log[1]['amount']     = $ipfs_reward;
                                    $user_balance_log[1]['before']     = $v['ipfs_wallet'];
                                    $user_balance_log[1]['after']      = $v['ipfs_wallet']+$ipfs_reward;
                                    $user_balance_log[1]['remark']     = '见点奖励';
                                    $admin_remark = "代数[{$generation}],直推人数[{$v['total_zt_person']}],来源用户ID[{$value['user_id']}],来源矿机ID[{$value['id']}]";
                                    $user_balance_log[1]['admin_remark']   = $admin_remark;
                                    $user_balance_log[1]['type']       = 1;
                                    $user_balance_log[1]['log_type']   = 7;
                                    $user_balance_log[1]['source']     = 4;
                                    $user_balance_log[1]['createtime'] = time();

                                    Db::name('user')->where('id',$v['id'])
                                    ->update([
                                    'movable_wallet' => Db::raw('movable_wallet+'.$last_invite_reward),
                                    'ipfs_wallet' => Db::raw('ipfs_wallet+'.$ipfs_reward),
                                    'total_jian_reward' => Db::raw('total_jian_reward+'.$invite_reward),
                                    ]);
                                    Db::name('user_balance_log')->insertAll($user_balance_log);
                                }
                        }
                        break;
                    default:
                            break;
                }

              } 
            }
             // file_put_contents('queue.txt', json_encode($all_user_miner).PHP_EOL, FILE_APPEND);
            Db::commit(); 
            return true;
        } catch (\think\Exception\DbException $exception){
            // 回滚事务
            Db::rollback();
            return false;
        }
      }
    }

    public function failed($data){
        file_put_contents('file.txt', time());
        // ...任务达到最大重试次数后，失败了
    }

}
